package eproject.mm.com.projectsupervision.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import eproject.mm.com.projectsupervision.PrjApplication;
import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.activity.ProblemInfoActivity;
import eproject.mm.com.projectsupervision.widget.IconFontTextview;

/**
 * Created by momotea on 2018/3/15.
 */

public class ProblemInfoDetailAdapter extends BaseAdapter {
    private List<Map> list;
    private Context context;
    private LayoutInflater inflater;

    public ProblemInfoDetailAdapter(Context context, List list){
        this.list = list;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Map getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        final Map item = getItem(position);

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.problem_info_item, null);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.iftv_icon = (IconFontTextview) convertView.findViewById(R.id.iftv_icon);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tv_person = (TextView) convertView.findViewById(R.id.tv_person);
            holder.rv_photos = (RecyclerView) convertView.findViewById(R.id.rv_photos);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_content.setText((String)item.get("describes"));
        holder.tv_time.setText((String)item.get("createtime"));

        String type = (String)item.get("type")+"";
        if(type.equals("0")){
            holder.tv_title.setText("问题整改");
        }else if(type.equals("1")){
            holder.tv_title.setText("整改复查");
        }

        holder.tv_person.setText((String)item.get("unitname")+" "+(String)item.get("creatorname"));


        String bImages = (String)item.get("newnames");
        String sImages = (String)item.get("newnames2");
        if(bImages != null && !bImages.equals("")){
            List<String> smallImagesList = new ArrayList<String>();
            List<String> bigImagesList = new ArrayList<String>();

            String[] smallImagesArray = sImages.split(",");
            String[] bigImagesArray = bImages.split(",");

            for(int i=0;i<smallImagesArray.length;i++){
                smallImagesList.add(smallImagesArray[i]);
                bigImagesList.add("http://"+ PrjApplication.getInstance().getServerIp()+":"+PrjApplication.getInstance().getServerPort() + bigImagesArray[i]);
            }
            BaseRecyclerImageViewAdapter imageViewAdapter = new BaseRecyclerImageViewAdapter(convertView.getContext(), smallImagesList, bigImagesList);
            holder.rv_photos.setLayoutManager(new GridLayoutManager(convertView.getContext(), 4));
            holder.rv_photos.getLayoutManager().setAutoMeasureEnabled(true);
            holder.rv_photos.setAdapter(imageViewAdapter);
            holder.rv_photos.setVisibility(View.VISIBLE);
        }else{
            holder.rv_photos.setVisibility(View.GONE);
        }

        return convertView;
    }

    private static class ViewHolder {
        TextView tv_content;
        TextView tv_time;
        TextView tv_title;
        TextView tv_person;
        RecyclerView rv_photos;
        IconFontTextview iftv_icon;
    }
}
